import { Component, OnInit, Output, Input, EventEmitter } from '@angular/core';
import { PagesService } from 'services/pages.service';
declare var layui: any;
declare var layer: any;
declare var $: any;
@Component({
  selector: 'app-cp-upload',
  templateUrl: './cp-upload.component.html',
  styleUrls: ['./cp-upload.component.scss']
})
export class CpUploadComponent implements OnInit {

  constructor(public interfaceService: PagesService) { }

  @Output() private outer = new EventEmitter();
  @Input() module: any;
  @Input() data: any;
  @Input() name: any;
  @Input() fileName: any;
  @Input() disabled: any;

  ngOnInit() {
    this.fileName = this.fileName === undefined ? 'file' : this.fileName;
    this.disabled = this.disabled === undefined ? false : this.disabled;
  }

  uploadFileSub(url, but, fun1?, fun2?, fun3?) {
    if (!this.module[this.data][this.fileName]) {
      delete this.module[this.data][this.fileName];
    }
    var formData = new FormData();
    for (var x in this.module[this.data]) {
      formData.append(x, this.module[this.data][x]);
    }
    $.ajax({
      type: 'post',
      url: url,
      data: formData,
      contentType: false,
      processData: false,
      success: (data: any) => {
        layer.msg(data.message);
        this.module[but] = false;
        if (this.interfaceService.codeValidation(data)) {
        if (data.code == 200) {
          if (fun1) {
            this.module[fun1]();
          }
          if (fun2) {
            this.module[fun2]();
          }
          if (fun3) {
            this.module[fun3]();
          }
        }
      }
    }
    });
  }

  uploadName = '';
  uploadFile(e): void {
    this.module[this.data][this.fileName] = e.target.files[0];
    this.module[this.data]['fileCp'] = this.module[this.data][this.fileName]['name'];
    this.uploadName = this.module[this.data][this.fileName]['name'];
  }


}
